package cn.szzsi._admin;

import cn.szzsi.common.model.AreaSystem;
import cn.szzsi.common.model.DeliveryType;
import cn.szzsi.common.model.Product;
import cn.szzsi.service.DeliveryAreaRelationService;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 商户后台类 - 配送方式 2016/9/27.
 */
public class DeliveryTypeController extends BaseAdminController<DeliveryType> {
    private DeliveryType deliveryType;
    private String shopId;

    // 添加
    public void add() {
        setAttr("allDeliveryMethod", getAllDeliveryMethod());
        setAttr("allWeightUnit", getAllWeightUnit());
        setAttr("allDeliveryCorp", DeliveryType.dao.getAllDeliveryCorp());
        render("delivery_type_input.html");
    }

    // 编辑
    public void edit() {
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("deliveryType", DeliveryType.dao.findById(id));
        }
        setAttr("allDeliveryMethod", getAllDeliveryMethod());
        setAttr("allWeightUnit", getAllWeightUnit());
        setAttr("allDeliveryCorp", DeliveryType.dao.getAllDeliveryCorp());

        setAttr("recordList", DeliveryAreaRelationService.service.getByDeliveryTypeId(id));
        render("delivery_type_input.html");
    }

    // 列表
    public void list() {
        shopId=getAttr("shopId");
        findBusinessByPage(shopId,"mall_delivery_type");
        render("delivery_type_list.html");
    }

    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        List<DeliveryType> lpa = DeliveryType.dao.find("select * from mall_delivery_type where uid = ? order by createDate desc",shopId);
        renderJson(lpa);
    }

    // 是否已存在 ajax验证
    public void checkName() {
        String value = getPara("deliveryType.name");
        if (isUnique("deliverytype", "name", value)) {
            renderText("true");
        } else {
            renderText("false");
        }
    }

    // 保存
    //@Before(DeliveryTypeValidator.class)
    public void save(){
        shopId=getAttr("shopId");
        deliveryType = getModel(DeliveryType.class);
        deliveryType.set("uid",shopId);
        //验证配送方式名称是否存在
        String deliveryTypeName=getPara("deliveryType.name");
        if(DeliveryType.getByNameAndUid(deliveryTypeName,shopId)!=null){
            addActionError("配送方式名称已存在");
            return;
        }
        String deliveryMethod = getPara("deliveryMethod","");
        if (StrKit.notBlank(deliveryMethod)){
            deliveryType.setDeliveryMethod(deliveryMethod);
        }

        String firstWeightUnit = getPara("firstWeightUnit","");
        if(StrKit.notBlank(firstWeightUnit)){
            deliveryType.setFirstWeightUnit(firstWeightUnit);
        }

        String continueWeightUnit = getPara("continueWeightUnit","");
        if(StrKit.notBlank(continueWeightUnit)){
            deliveryType.setContinueWeightUnit(continueWeightUnit);
        }
        saved(deliveryType);

        //更新配送方式和地区关系表
        Integer []regions= getParaValuesToInt("regions[]");
        if(regions!=null){
            DeliveryAreaRelationService.service.update(regions,deliveryType.getId(),shopId);
        }else{
            Db.update("delete from mall_delivery_area_relation where deliveryTypeId=?",deliveryType.getId());//删除配送方式地区关联表
        }
        //更新配送方式和地区关系表
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    // 更新
    public void update(){
        shopId=getAttr("shopId");
        deliveryType = getModel(DeliveryType.class);
        String deliveryMethod = getPara("deliveryMethod","");
        if (StrKit.notBlank(deliveryMethod)){
            deliveryType.setDeliveryMethod(deliveryMethod);
        }

        String firstWeightUnit = getPara("firstWeightUnit","");
        if(StrKit.notBlank(firstWeightUnit)){
            deliveryType.setFirstWeightUnit(firstWeightUnit);
        }

        String continueWeightUnit = getPara("continueWeightUnit","");
        if(StrKit.notBlank(continueWeightUnit)){
            deliveryType.setContinueWeightUnit(continueWeightUnit);
        }

        if (deliveryType.getBigDecimal("firstWeightPrice").compareTo(new BigDecimal("0")) < 0) {
            addActionError("首重价格不允许小于0");
            return;
        }

        if (deliveryType.getBigDecimal("continueWeightPrice").compareTo(new BigDecimal("0")) < 0) {
            addActionError("续重价格不允许小于0");
            return;
        }
        updated(deliveryType);
        //更新配送方式和地区关系表
        Integer []regions= getParaValuesToInt("regions[]");
        if(regions!=null){
            DeliveryAreaRelationService.service.update(regions,deliveryType.getId(),shopId);
        }else{
            Db.update("delete from mall_delivery_area_relation where deliveryTypeId=?",deliveryType.getId());//删除配送方式地区关联表
        }

        //更新配送方式和地区关系表
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
    }

    // 删除
    @Before(Tx.class)
    public void delete() {
        String id=getPara("id");
        //long totalCount = DeliveryType.dao.getTotalCount();
        DeliveryType.dao.deleteById(id);
        Db.update("delete from mall_delivery_area_relation where deliveryTypeId=?",id);//删除配送方式地区关联表
        renderJson(Ret.ok());
    }

    public void getAreaByParent(){
        shopId=getAttr("shopId");
        String parentId=getPara("parent_id");
        if("00".equals(parentId)){
            parentId=null;
        }
        List<AreaSystem> areaSystemList= AreaSystem.dao.getExpectByParent(parentId,shopId);
        List expectList=new ArrayList();
        for(AreaSystem areaSystem:areaSystemList){
            expectList.add("<option value='"+areaSystem.getId()+"'>"+areaSystem.getName()+"</option>");
        }
        renderJson(expectList);
    }


    // 获取所有配送类型
    public List<DeliveryType.DeliveryMethod> getAllDeliveryMethod() {
        List<DeliveryType.DeliveryMethod> allDeliveryMethod = new ArrayList<DeliveryType.DeliveryMethod>();
        for (DeliveryType.DeliveryMethod deliveryMethod : DeliveryType.DeliveryMethod.values()) {
            allDeliveryMethod.add(deliveryMethod);
        }
        return allDeliveryMethod;
    }

    // 获取所有重量单位
    public List<Product.WeightUnit> getAllWeightUnit() {
        List<Product.WeightUnit> allWeightUnit = new ArrayList<Product.WeightUnit>();
        for (Product.WeightUnit weightUnit : Product.WeightUnit.values()) {
            allWeightUnit.add(weightUnit);
        }
        return allWeightUnit;
    }
}
